<?php

class Application_Model_Product
{
    /**
     * @param int $limit
     * @return ProductPOJO[]
     */
    public static function getLastCreatedProducts($limit = 5)
    {
        $dbAdapter = JDbAdapter::factory();

        $productPOJOs = array();
        $assoc = $dbAdapter->fetchAll(
            $dbAdapter->select()
                ->from('PRODUCTS')
                ->where('Deleted = ?', false)
                ->order('CreatedDate DESC')
                ->limit($limit)
        );
        foreach ($assoc as $row) {
            $productPOJO = new ProductPOJO($row);
            array_push($productPOJOs, $productPOJO);
        }

        return $productPOJOs;
    }

    /**
     * @param bool $deleted
     * @return ProductPOJO[]
     */
    public static function getProducts($deleted = false)
    {
        $dbAdapter = JDbAdapter::factory();

        $productPOJOs = array();
        $assoc = $dbAdapter->fetchAll(
            $dbAdapter->select()
                ->from('PRODUCTS')
                ->where('Deleted = ?', $deleted)
        );
        foreach ($assoc as $row) {
            $productPOJO = new ProductPOJO($row);
            array_push($productPOJOs, $productPOJO);
        }

        return $productPOJOs;
    }

    /**
     * @param int $id
     * @param bool $deleted
     * @return ProductPOJO
     */
    public static function getProduct($id, $deleted = false)
    {
        $dbAdapter = JDbAdapter::factory();

        $row = $dbAdapter->fetchRow(
            $dbAdapter->select()->from('PRODUCTS')
                ->where('ID = ?', $id)
                ->where('Deleted = ?', $deleted)
        );
        if ($row == null) {
            return null;
        }

        return new ProductPOJO($row);
    }

    /**
     * @param string $name
     * @param string $description
     * @param string|Zend_Date $expired
     * @param int $orginalAmount
     * @param int $oldPrice
     * @param int $newPrice
     * @param float $lat
     * @param float $lng
     * @param int $productTypeID
     * @return int
     */
    public static function addProduct($name, $description, $expired, $orginalAmount,
                                      $oldPrice, $newPrice, $lat, $lng, $productTypeID)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            if ($expired instanceof Zend_Date) {
                $expired = $expired->get('y-M-d H:m:s');
            }

            $dbAdapter->insert(
                'PRODUCTS',
                array(
                    'Name' => $name,
                    'Description' => $description,
                    'Expired' => $expired,
                    'OrginalAmount' => $orginalAmount,
                    'ExistingAmount' => $orginalAmount, // Existing Amount = Orginal Amount
                    'OldPrice' => $oldPrice,
                    'NewPrice' => $newPrice,
                    'Lat' => $lat,
                    'Lng' => $lng,
                    'ProductTypeID' => $productTypeID
                )
            );

            return (int)$dbAdapter->lastInsertId('PRODUCTS');
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return 0;
        }
    }

    /**
     * @param int $id
     * @param string $name
     * @param string $description
     * @param string|Zend_Date $expired
     * @param int $orginalAmount
     * @param int $oldPrice
     * @param int $newPrice
     * @param float $lat
     * @param float $lng
     * @param int $productTypeID
     * @return bool
     */
    public static function updateProduct($id, $name, $description, $expired, $orginalAmount,
                                         $oldPrice, $newPrice, $lat, $lng, $productTypeID)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            if ($expired instanceof Zend_Date) {
                $expired = $expired->get('y-M-d H:m:s');
            }

            $numberOfAffects = $dbAdapter->update(
                'PRODUCTS',
                array(
                    'Name' => $name,
                    'Description' => $description,
                    'Expired' => $expired,
                    'OrginalAmount' => $orginalAmount,
                    'OldPrice' => $oldPrice,
                    'NewPrice' => $newPrice,
                    'Lat' => $lat,
                    'Lng' => $lng,
                    'ProductTypeID' => $productTypeID
                ),
                "ID = $id"
            );

            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }

    /**
     * @param int[] $ids
     * @return bool
     */
    public static function deleteProducts($ids)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $str = '(';
            $length = count($ids);
            for ($i = 0; $i < $length - 1; $i++) {
                $str .= $ids[$i] . ', ';
            }
            $str .= $ids[$length - 1];
            $str .= ')';
            $numberOfAffects = $dbAdapter->update(
                'PRODUCTS', array('Deleted' => true), "ID IN $str"
            );
            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }

    /**
     * @param int $id
     * @return bool
     */
    public static function deleteProduct($id)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $numberOfAffects = $dbAdapter->update(
                'PRODUCTS', array('Deleted' => true), "ID = $id"
            );
            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }
}

class ProductPOJO extends POJO
{
    /** @var int */
    public $ID;

    /** @var string */
    public $Name;

    /** @var string */
    public $Image;

    /** @var string */
    public $Description;

    /** @var Zend_Date */
    public $CreatedDate;

    /** @var Zend_Date */
    public $Expired;

    /** @var int */
    public $OrginalAmount;

    /** @var int */
    public $ExistingAmout;

    /** @var int */
    public $OldPrice;

    /** @var int */
    public $NewPrice;

    /** @var float */
    public $Lat;

    /** @var float */
    public $Lng;

    /** @var bool */
    public $Deleted;

    /** @var int */
    public $ProductTypeID;

    /** @var int */
    public $UserID;

    /**
     * @return void
     */
    protected function convert()
    {
        $this->ID = (int)$this->ID;
        $this->Name = (string)$this->Name;
        $this->Image = (string)$this->Image;
        $this->Description = (string)$this->Description;
        $this->CreatedDate = new Zend_Date($this->CreatedDate);
        $this->Expired = new Zend_Date($this->Expired);
        $this->OrginalAmount = (int)$this->OrginalAmount;
        $this->ExistingAmout = (int)$this->ExistingAmout;
        $this->OldPrice = (int)$this->OldPrice;
        $this->NewPrice = (int)$this->NewPrice;
        $this->Lat = (float)$this->Lat;
        $this->Lng = (float)$this->Lng;
        $this->Deleted = (bool)$this->Deleted;
        $this->ProductTypeID = (int)$this->ProductTypeID;
        $this->UserID = (int)$this->UserID;
    }
}